<template>
  <div class="div-max">
    <div class="footer_wrap">
      <div class="finance-2">
        <div>金融WEB</div>
        <div>值得信赖的投资理财平台</div>
      </div>
    </div>

    <div class="finance-3">
      <div class="u3690_text">
        <p>
          <span> 设置新登录密码 </span>
        </p>
      </div>

      <div class="progress-div">
        <el-steps :active="active" finish-status="success" align-center>
          <el-step title="身份验证"></el-step>
          <el-step title="设置新密码"></el-step>
          <el-step title="完成"></el-step>
        </el-steps>
      </div>

      <div class="finance-4" v-if="div4">
        <el-form ref="form" :rules="rules" :model="form">
          <div class="grid-content bg-purple">
            <el-form-item
              prop="user_phone"
              class="pr"
              :rules="[
                {
                  pattern: /^1[3456789]\d{9}$/,
                  message: '请输入有效的手机号码!',
                  trigger: 'change',
                },
              ]"
            >
              <el-input
                v-model="form.user_phone"
                placeholder="手机号"
                prefix-icon="el-icon-user"
                clearable
              ></el-input>
            </el-form-item>
          </div>

          <div class="grid-content bg-purple">
            <el-form-item prop="phoneCode" class="pr">
              <el-input
                v-model="yzm"
                placeholder="短信验证码"
                prefix-icon="el-icon-mobile-phone"
                style="width: 72%"
                clearable
              ></el-input
              >&nbsp;&nbsp;&nbsp;
              <button
                @click.prevent="getShortMessageCode()"
                :class="{ code_btn: ppt, code_btn1: !ppt }"
                :disabled="!show"
              >
                <span v-show="show">获取验证码</span>
                <span v-show="!show">{{ count }} 秒后可重发</span>
              </button>
            </el-form-item>
          </div>
        </el-form>

        <div class="div-button">
          <el-button type="danger" @click="submitForm()" style="width: 100%"
            >下一步</el-button
          >
        </div>
      </div>

      <div class="finance-4" v-if="div5">
          <el-form ref="ruleForm" :rules="rules" :model="ruleForm">
          <div class="grid-content bg-purple">
            <el-form-item prop="user_pwd" class="pr">
              <el-input
                v-model="ruleForm.user_pwd"
                placeholder="密码"
                prefix-icon="el-icon-lock"
                show-password
                clearable
              ></el-input>
            </el-form-item>
          </div>

          <div class="grid-content bg-purple">
            <el-form-item prop="user_pwds" class="pr">
              <el-input
                v-model="ruleForm.user_pwds"
                placeholder="确认密码"
                prefix-icon="el-icon-lock"
                show-password
                clearable
              ></el-input>
            </el-form-item>
          </div>
        </el-form>

        <div v-if="button2" class="div-button">
          <el-button type="danger" @click="submitForms()" style="width: 100%"
            >下一步</el-button
          >
        </div>
      </div>

      <div class="finance-5" v-if="div6">
        <div v-if="button3" class="div-button">
          <div class="imgdiv">
            <img src="./assets/u3778.png" alt="" />
          </div>

          <div>
            <p>
              <span> 恭喜你，新登录密码设置成功 </span>
            </p>
          </div>

          <div class="bu">
            <el-button type="danger" @click="submitFormss()" style="width: 100%"
              >立即登录</el-button
            >
          </div>
        </div>
      </div>
    </div>

    <Bottom />
  </div>
</template>

<script>
import Bottom from "./finance/Bottom.vue";
import axios from "axios";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.user_pwds !== "") {
          this.$refs.ruleForm.validateField("user_pwds");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.user_pwd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      changePwdDialog: false, //修改密码弹框
      ruleForm: {
        user_pwd: "",
        user_pwds: "",
      },
      form: {
        user_phone: "",
      },

      // user_pwd: "",
      // user_pwds: "",
      yzm: "",
      checked: false,
      show: true,
      timer: 0,
      count: 0,
      ppt: true, //点击时改变样式
      active: 0,
      div4: true,
      div5: false,
      div6: false,
      button1: true,
      button2: false,
      button3: false,
      rules: {
			user_pwd: [
			{ required: true, message: '请输入密码', trigger: 'blur' },
			{ min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },
			{ validator: validatePass, trigger: 'blur' }
			],
			user_pwds:[
			{ required: true, message: '请确认密码', trigger: 'blur' },
			{ min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' },
			{ validator: validatePass2, trigger: 'blur', required: true }
			],
		}
	
    };
  },
  components: {
    Bottom,
  },
  methods: {
    // 获取短信验证码
    getShortMessageCode() {
      this.ppt = !this.ppt;
      //axios请求
      // console.log(this.form.phone);
      // console.log(1111111111111);
      // console.log(!this.timer);
      axios({
        url: "/api/user/smsverification",
        params: {
          phone: this.user_phone,
        },
      });
      // 验证码倒计时
      if (!this.timer) {
        console.log(this.timer);
        this.count = 60;
        this.show = false;
        this.timer = setInterval(() => {
          if (this.count > 0 && this.count <= 60) {
            this.count--;
          } else {
            this.show = true;
            clearInterval(this.timer);
            this.timer = null;
            this.ppt = !this.ppt;
          }
        }, 1000);
      }
    },

    //下一步
    submitForm() {
      if (this.form.user_phone == null || this.form.user_phone.length != 11) {
        this.$message({
          type: "warning",
          message: "请输入正确的手机号",
        });
        return;
      } else {
        // this.div4 = false;
        // this.div5 = true;
        // this.button1 = false;
        // this.button2 = true;
        // if (this.active++ > 2) this.active = 0;
        axios({
          url: "api/user/updateUserPwd",
          method: "POST",
          params: {
            user_phone: this.form.user_phone,
            code: this.yzm,
          },
        }).then((res) => {
          if (res.data.code == 200) {
            this.div4 = false;
            this.div5 = true;
            this.button1 = false;
            this.button2 = true;
            if (this.active++ > 2) this.active = 0;
          } else {
            this.$message({
              type: "warning",
              message: res.data.message,
            });
            return;
          }
        });
      }
    },

    submitForms() {
      if (this.ruleForm.user_pwd == "" || this.ruleForm.user_pwd.length < 6) {
        this.$message({
          type: "success",
          message: "请输入六位以上的密码",
        });
        return;
      } else {
        if (this.ruleForm.user_pwd != this.ruleForm.user_pwds) {
          this.$message({
            type: "success",
            message: "两次密码不一样",
          });
          return;
        } else {
          alert(this.form.user_phone);
          axios({
            url: "api/user/updateUserPassward",
            method: "POST",
            params: {
              user_phone: this.form.user_phone,
              user_pwd: this.ruleForm.user_pwd,
            },
          }).then((res) => {
            if (res.data.code == 200) {
              this.div5 = false;
              this.div6 = true;
              this.button3 = true;
              if (this.active++ > 2) this.active = 0;
              if (this.active++ > 2) this.active = 0;
            } else {
              this.$message({
                type: "warning",
                message: res.data.message,
              });
              return;
            }
          });
        }
      }
    },

    submitFormss() {
      this.$router.push({ path: "login" });
    },
  },
};
</script>

<style scoped>
.finance-2 {
  float: left;
  /* background-color: rgb(43, 210, 49); */
  margin-top: 17px;
  margin-left: 28%;
}
.finance-2 div:nth-child(1) {
  font-size: 190%;
  color: red;
  font-weight: bold;
}
.finance-2 div:nth-child(2) {
  font-size: 90%;
}
.finance_size {
  font-size: 100%;
  margin-left: 400px;
  line-height: 100px;
  float: left;
}
.top {
  padding-top: 62px;
}
.footer_wrap {
  /* position: fixed; */
  left: 0;
  top: 0;
  display: flex;
  width: 100%;
  height: 100px;
  text-align: center;
  background-color: #333;
  color: #ccc;
  float: left;
}

.footer_wrap .router-link-active {
  color: red;
}
.div-max {
  background-color: #f6f6f6;
  float: left;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
  margin-top: 20px;
}
.code_btn {
  height: 37px;
  background-color: red;
  color: white;
  border: 1px solid red;
  background-color: red;
  -webkit-border-radius: 5px;
}
.code_btn1 {
  height: 37px;
  background-color: red;
  color: white;
  border: 1px solid #cccccc;
  background-color: #cccccc;
  -webkit-border-radius: 5px;
}
a {
  text-decoration: none; /* 去除默认的下划线 */
  color: #17acf7;
}

.div-p {
  position: absolute;
  float: left;
  transform: translate(650%, -90%);
}
.finance-3 {
  width: 1120px;
  height: 600px;
  margin-top: 60px;
  margin-left: 195px;
  margin-bottom: 50px;
  background-color: white;
  float: left;
}
.u3690_text {
  border-width: 0px;
  position: absolute;
  left: 250px;
  top: 200px;
  width: 250px;
  height: 30px;
  font-family: "微软雅黑";
  font-weight: 400;
  font-style: normal;
  font-size: 25px;
  color: rgb(15, 14, 14);
}
.finance-4 {
  position: absolute;
  width: 386px;
  height: 290px;
  left: 580px;
  top: 420px;
  background-color: transparent;
}
.finance-5 {
  position: absolute;
  width: 386px;
  height: 290px;
  left: -400px;
  top: 800px;
  background-color: transparent;
}
.div-password {
  margin-top: 43.5px;
}
.div-yzm {
  margin-top: 43.5px;
}
.div-button {
  margin-top: 43.5px;
}
.div-hqyzm {
  position: absolute;
  left: 70%;
  top: 57.6%;
}
.login-yes {
  position: absolute;
  margin-top: 20px;
  top: 90.6%;
}
.el-step__title.is-wait {
  color: rgb(14, 188, 72);
}
.progress-div {
  position: absolute;
  width: 40%;
  left: 32%;
  top: 40%;
}
.finance-5 p span {
  position: absolute;
  left: 20%;
  top: 10%;
  font-family: "微软雅黑 Bold", "微软雅黑 Regular", "微软雅黑";
  font-weight: 700;
}

.imgdiv {
  position: absolute;
  width: 40%;
  left: 45%;
  top: -10%;
}
.bu {
  position: absolute;
  width: 386px;
  top: 40%;
}
</style>